const hre = require("hardhat");

async function main() {
  console.log("Testing BSCLaunchPad contract interaction...");
  
  const CONTRACT_ADDRESS = "0x3aa0359e3aCB5E8eAD635164FACFFa3a39D87279";
  
  // Get the deployer signer
  const [deployer] = await hre.ethers.getSigners();
  console.log("Testing with account:", deployer.address);
  console.log("Account balance:", hre.ethers.formatEther(await hre.ethers.provider.getBalance(deployer.address)), "BNB");
  
  // Get the contract instance
  const BSCLaunchPad = await hre.ethers.getContractFactory("BSCLaunchPad");
  const launchPad = BSCLaunchPad.attach(CONTRACT_ADDRESS);
  
  console.log("\n=== Contract Information ===");
  
  // Check contract constants
  console.log("Platform Fee Percent:", await launchPad.platformFeePercent());
  console.log("Min Target:", hre.ethers.formatEther(await launchPad.MIN_TARGET()), "BNB");
  console.log("Max Target:", hre.ethers.formatEther(await launchPad.MAX_TARGET()), "BNB");
  console.log("Launch Duration:", (await launchPad.LAUNCH_DURATION()).toString(), "seconds");
  console.log("Graduation Threshold:", await launchPad.GRADUATION_THRESHOLD(), "%");
  console.log("Base Price:", hre.ethers.formatEther(await launchPad.CURVE_BASE_PRICE()), "BNB");
  console.log("Price Increment:", hre.ethers.formatEther(await launchPad.CURVE_PRICE_INCREMENT()), "BNB");
  
  // Get next launch ID
  const nextLaunchId = await launchPad.nextLaunchId();
  console.log("Next Launch ID:", nextLaunchId.toString());
  
  console.log("\n=== Testing Token Creation ===");
  
  try {
    // Create a test token
    const tx = await launchPad.createTokenLaunch(
      "TestToken",                           // name
      "TEST",                               // symbol
      "A test token for BSC LaunchPad",     // description
      "https://testtoken.com",              // website
      "https://t.me/testtoken",             // telegram
      "https://twitter.com/testtoken",      // twitter
      hre.ethers.parseEther("10"),          // target amount: 10 BNB
      { value: hre.ethers.parseEther("0.01") } // creation fee: 0.01 BNB
    );
    
    console.log("Transaction hash:", tx.hash);
    console.log("Waiting for confirmation...");
    
    const receipt = await tx.wait();
    console.log("Token creation confirmed!");
    console.log("Gas used:", receipt.gasUsed.toString());
    
    // Get launch info
    const launchInfo = await launchPad.getLaunchInfo(nextLaunchId);
    console.log("\n=== Launch Information ===");
    console.log("Token Address:", launchInfo.tokenAddress);
    console.log("Creator:", launchInfo.creator);
    console.log("Name:", launchInfo.name);
    console.log("Symbol:", launchInfo.symbol);
    console.log("Description:", launchInfo.description);
    console.log("Target Amount:", hre.ethers.formatEther(launchInfo.targetAmount), "BNB");
    console.log("Raised Amount:", hre.ethers.formatEther(launchInfo.raisedAmount), "BNB");
    console.log("Current Price:", hre.ethers.formatEther(launchInfo.currentPrice), "BNB");
    console.log("Tokens Sold:", hre.ethers.formatEther(launchInfo.tokensSold));
    console.log("Is Launched:", launchInfo.launched);
    console.log("Is Cancelled:", launchInfo.cancelled);
    
    // Test buy preview
    const buyPreview = await launchPad.getBuyPreview(nextLaunchId, hre.ethers.parseEther("1"));
    console.log("\n=== Buy Preview (1 BNB) ===");
    console.log("Tokens Received:", hre.ethers.formatEther(buyPreview.tokensReceived));
    console.log("New Price:", hre.ethers.formatEther(buyPreview.newPrice), "BNB");
    
  } catch (error) {
    console.error("Error creating token:", error.message);
  }
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
